package com.syedu.day20;

import java.io.*;
import java.net.Socket;

/**
 * @author Administrator
 * @Description 文件上传客户端
 * @createTime 2022/4/12 13:35
 */
public class FileUploadClient {
    public static void main(String[] args) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try (Socket socket = new Socket("127.0.0.1", 9999)) {
            //创建流对象,先读,后写
            //a.从客户端本地文件中读取数据到程序中输入流
            bis = new BufferedInputStream(new FileInputStream("F:\\JavaSE.zip"));
            //b.把数据发送到服务端的输出流
            bos = new BufferedOutputStream(socket.getOutputStream());
            //c.接受服务端发送的完成信息流
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //读写数据
            byte[] buf = new byte[3 * 1024];
            int len;
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
                bos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
